package views;

import controller.UserController;
import entity.User;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class UserView {
    BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
    protected UserController userController=new UserController();
    public boolean customerLoginView(){
        try {
            System.out.println("欢迎进入用户登录界面");
            System.out.println("请输入账号：");
            String id=bufferedReader.readLine();
            System.out.println("请输入密码：");
            String password=bufferedReader.readLine();
            return userController.customerLogin(Integer.parseInt(id),password);
        }catch (IOException e){
            System.out.println("io异常，登录失败！");
            return false;
        }
    }
    public boolean sellerLoginView(){
        try {
            System.out.println("欢迎进入商户登录界面");
            System.out.println("请输入账号：");
            String id=bufferedReader.readLine();
            System.out.println("请输入密码：");
            String password=bufferedReader.readLine();
            return userController.sellerLogin(Integer.parseInt(id),password);
        }catch (IOException e){
            System.out.println("io异常，登录失败！");
            return false;
        }
    }
    public boolean administratorLoginView(){
        try {
            System.out.println("欢迎进入管理员登录界面");
            System.out.println("请输入账号：");
            String id=bufferedReader.readLine();
            System.out.println("请输入密码：");
            String password=bufferedReader.readLine();
            return userController.administratorLogin(Integer.parseInt(id),password);
        }catch (IOException e){
            System.out.println("io异常,登录失败！");
            return false;
        }
    }
    public boolean registerCustomerView(){
        try {
            System.out.println("欢迎进入用户注册界面");
            System.out.println("请输入您想要的账号：");
            String id=bufferedReader.readLine();
            User user=userController.selectUserById(Integer.parseInt(id));
            if (user==null){
                System.out.println("请输入您的用户名：");
                String userName=bufferedReader.readLine();
                System.out.println("请输入您的密码");
                String passWord=bufferedReader.readLine();
                System.out.println("请输入您的密保问题：");
                String question=bufferedReader.readLine();
                System.out.println("请输入您的密保答案：");
                String answer=bufferedReader.readLine();
                User user1=new User(Integer.parseInt(id),userName,passWord,question,answer,1);
                System.out.println(user1);
                return userController.registerUser(user1);
            }else{
                System.out.println("该账号已存在,注册失败！");
                return false;
            }
        }catch (IOException e){
            System.out.println("io异常,注册失败！");
            return false;
        }
    }

    /**
     *
     * @return
     */
    public boolean registerSellerView(){
        try {
            System.out.println("欢迎进入商户注册界面");
            System.out.println("请输入您想要的账号：");
            String id=bufferedReader.readLine();
            User user=userController.selectUserById(Integer.parseInt(id));
            if (user==null){
                System.out.println("请输入您的用户名：");
                String userName=bufferedReader.readLine();
                System.out.println("请输入您的密码");
                String passWord=bufferedReader.readLine();
                System.out.println("请输入您的密保问题：");
                String question=bufferedReader.readLine();
                System.out.println("请输入您的密保答案：");
                String answer=bufferedReader.readLine();
                User user1=new User(Integer.parseInt(id),userName,passWord,question,answer,2);
                System.out.println(user1);
                return userController.registerUser(user1);
            } else{
                System.out.println("该id已存在,注册失败！");
                return false;
            }
        }catch (IOException e){
            System.out.println("io异常,登录失败！");
            return false;
        }
    }

    /**
     *
     * @return
     */
    public boolean insertUserView(){
        try {
            System.out.println("欢迎进入增加用户界面");
            System.out.println("请输入想要添加的用户id");
            String id=bufferedReader.readLine();
            User user=userController.selectUserById(Integer.parseInt(id));
            if (user==null){
                System.out.println("请输入想要添加的用户名：");
                String userName=bufferedReader.readLine();
                System.out.println("请输入密码：");
                String passWord=bufferedReader.readLine();
                System.out.println("请输入用户权限：");
                String level=bufferedReader.readLine();
                User user1=new User(Integer.parseInt(id),userName,passWord,null,null,Integer.parseInt(level));
                System.out.println(user1);
                return userController.insertUser(user1);
            }else
                System.out.println("该账号已存在，无法添加！");
        }catch (IOException e){
            System.out.println("io异常，添加失败！");
            return false;
        }
        return false;
    }

    /**
     *
     * @return
     */
    public boolean deleteUserView(){
        try {
            System.out.println("欢迎进入删除用户界面");
            System.out.println("请输入想要删除的用户id");
            String id= null;
            id = bufferedReader.readLine();
            return userController.deleteUserById(Integer.parseInt(id));
        } catch (IOException e) {
            System.out.println("io异常，删除失败！");
            return false;
        }
    }

    /**
     *
     * @return
     */
    public boolean updateUserView(){
        try {
            System.out.println("欢迎进入更改用户信息界面");
            System.out.println("请输入您要更改的用户账号：");
            String id=bufferedReader.readLine();
            System.out.println("请输入该用户的密保答案：");
            String answer=bufferedReader.readLine();
            User user=userController.selectUserById(Integer.parseInt(id));
            if (answer.equals(user.getAnswer())){
                System.out.println("请输入新的用户名");
                String userName=bufferedReader.readLine();
                System.out.println("请输入新密码：");
                String password=bufferedReader.readLine();
                User user1=new User(user.getId(),userName,password,user.getQuestion(),user.getAnswer(),user.getLevel());
                return userController.updateUserById(Integer.parseInt(id),user1);
            }else
                System.out.println("密保答案错误，更改失败！");
        }catch (IOException e){
            System.out.println("io异常，更改失败！");
            return false;
        }
        return false;
    }

    /**
     *
     * @return
     */
    public void selectUserViewO(){
        try {
            System.out.println("********欢迎来到查询页面！********");
            System.out.println("请选择你要选择的查询方式：");
            System.out.println(" 1.查所有，2.根据id查询，3.根据用户名查询 ");
            switch (Integer.parseInt(bufferedReader.readLine())){
                case 1:
                    List<User> userList=userController.selectAllUser();
                    System.out.println("查询的结果是：");
                    System.out.println(userList.toString());
                    break;
                case 2:
                    System.out.println("请输入你想要查询的id");
                    String id=bufferedReader.readLine();
                    User user=userController.selectUserById(Integer.parseInt(id));
                    System.out.println("查询的结果是：");
                    System.out.println(user.toString());
                    break;
                case 3:
                    System.out.println("请输入你想查询的用户名");
                    String userName=bufferedReader.readLine();
                    User user1=userController.selectUserByUserName(userName);
                    System.out.println("查询的结果是：");
                    System.out.println(user1.toString());
                    break;
                default:
                    System.out.println("输入指令错误！");
            }
        }catch (IOException e){
            System.out.println("查询失败！");
        }
    }
}
